/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Telas;

import SYSGEST.br.fornecedor.Fornecedor;
import SYSGEST.br.fornecedor.FornecedorDAO;
import SYSGEST.br.fornecedor.FornecedorModel;
import javax.swing.JOptionPane;

/**
 *
 * @author Mesopotanea
 */
public class TelaBuscarForne extends javax.swing.JDialog {

    /**
     * Creates new form TelaBuscar
     */
    public TelaBuscarForne() {
        initComponents();

        FornecedorDAO DAO = new FornecedorDAO();
        FornecedorModel fm = new FornecedorModel(DAO.listar());
        tbPesquisaFornecedor.setModel(fm);

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        edtPesquisarFornecedor = new javax.swing.JTextField();
        jcTipoPesquisar = new javax.swing.JComboBox();
        btBuscarFornecedor = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbPesquisaFornecedor = new javax.swing.JTable();
        btNovoPesq = new javax.swing.JButton();
        btEditPesq = new javax.swing.JButton();
        btExcPesq = new javax.swing.JButton();
        btSairPesq = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel2.setBackground(new java.awt.Color(0, 0, 255));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Pesquisar Fornecedor");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(137, 137, 137)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addContainerGap(12, Short.MAX_VALUE))
        );

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 13)); // NOI18N
        jLabel2.setText("Pesquisar.:");

        jcTipoPesquisar.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Nome", "CNPJ" }));

        btBuscarFornecedor.setText("Buscar");
        btBuscarFornecedor.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btBuscarFornecedorMouseClicked(evt);
            }
        });
        btBuscarFornecedor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btBuscarFornecedorActionPerformed(evt);
            }
        });

        tbPesquisaFornecedor.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Codigo", "Produto"
            }
        ));
        jScrollPane1.setViewportView(tbPesquisaFornecedor);

        btNovoPesq.setFont(new java.awt.Font("Arial Black", 1, 13)); // NOI18N
        btNovoPesq.setText("Novo");
        btNovoPesq.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btNovoPesqActionPerformed(evt);
            }
        });

        btEditPesq.setFont(new java.awt.Font("Arial Black", 1, 13)); // NOI18N
        btEditPesq.setText("Editar");
        btEditPesq.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btEditPesqActionPerformed(evt);
            }
        });

        btExcPesq.setFont(new java.awt.Font("Arial Black", 1, 13)); // NOI18N
        btExcPesq.setText("Excluir");
        btExcPesq.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btExcPesqActionPerformed(evt);
            }
        });

        btSairPesq.setFont(new java.awt.Font("Arial Black", 1, 13)); // NOI18N
        btSairPesq.setText("Voltar");
        btSairPesq.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSairPesqActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(btNovoPesq)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btEditPesq)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btExcPesq)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btSairPesq))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel2)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jcTipoPesquisar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(edtPesquisarFornecedor, javax.swing.GroupLayout.PREFERRED_SIZE, 251, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(btBuscarFornecedor))))
                .addContainerGap(15, Short.MAX_VALUE))
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btEditPesq, btExcPesq, btNovoPesq, btSairPesq});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(edtPesquisarFornecedor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jcTipoPesquisar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btBuscarFornecedor))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(btNovoPesq)
                    .addComponent(btEditPesq)
                    .addComponent(btExcPesq)
                    .addComponent(btSairPesq))
                .addGap(167, 167, 167))
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {btEditPesq, btExcPesq, btNovoPesq, btSairPesq});

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 218, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void btSairPesqActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSairPesqActionPerformed
        // Chama tela fornecedor
        TelaFornecedor tfn = new TelaFornecedor();
        dispose();
        tfn.setVisible(true);
    }//GEN-LAST:event_btSairPesqActionPerformed

    private void btBuscarFornecedorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btBuscarFornecedorActionPerformed
        // TODO add your handling code here:
        FornecedorDAO dao = new FornecedorDAO();
        FornecedorModel atm = new FornecedorModel(dao.pesquisarNome(edtPesquisarFornecedor.getText()));
        tbPesquisaFornecedor.setModel(atm);


    }//GEN-LAST:event_btBuscarFornecedorActionPerformed

    private void btNovoPesqActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btNovoPesqActionPerformed
        // TODO add your handling code here:
        TelaFornecedor tf = new TelaFornecedor();
        dispose();
        tf.setVisible(true);
    }//GEN-LAST:event_btNovoPesqActionPerformed

    private void btEditPesqActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btEditPesqActionPerformed
       int linha = tbPesquisaFornecedor.getSelectedRow();
        if (linha == -1) {
            JOptionPane.showMessageDialog(rootPane,
                    "Selecione uma linha a ser editada!");
        } else {
            Fornecedor fornecedores = new Fornecedor();
            int codigo = (int) tbPesquisaFornecedor.getValueAt(linha, 0);

            FornecedorDAO dao = new FornecedorDAO();
            fornecedores = dao.pesquisacodigo(codigo);

            TelaFornecedor tela = new TelaFornecedor(fornecedores);
            tela.setVisible(true);
            dispose();
        }

    }//GEN-LAST:event_btEditPesqActionPerformed

    private void btExcPesqActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btExcPesqActionPerformed
        int linha = tbPesquisaFornecedor.getSelectedRow();
        if (linha == -1) {
            JOptionPane.showMessageDialog(rootPane,
                    "Selecione uma linha a ser excluída!");
        } else {
            if (JOptionPane.showConfirmDialog(rootPane,
                    "Deseja excluir o fornecedor selecionado?",
                    "Excluir?", JOptionPane.YES_NO_OPTION)
                    == JOptionPane.YES_OPTION) {
                Fornecedor fornecedores = new Fornecedor();
                int codigo = (int) tbPesquisaFornecedor.getValueAt(linha, 0);

                FornecedorDAO dao = new FornecedorDAO();
                fornecedores = dao.pesquisacodigo(codigo);

                dao.excluir(fornecedores);
            }
            FornecedorDAO dao = new FornecedorDAO();
            FornecedorModel ltm = new FornecedorModel(dao.listar());
            tbPesquisaFornecedor.setModel(ltm);
        }
    }//GEN-LAST:event_btExcPesqActionPerformed

    private void btBuscarFornecedorMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btBuscarFornecedorMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_btBuscarFornecedorMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TelaBuscarForne.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TelaBuscarForne.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TelaBuscarForne.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TelaBuscarForne.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new TelaBuscarForne().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btBuscarFornecedor;
    private javax.swing.JButton btEditPesq;
    private javax.swing.JButton btExcPesq;
    private javax.swing.JButton btNovoPesq;
    private javax.swing.JButton btSairPesq;
    private javax.swing.JTextField edtPesquisarFornecedor;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JComboBox jcTipoPesquisar;
    private javax.swing.JTable tbPesquisaFornecedor;
    // End of variables declaration//GEN-END:variables
}
